<% include header.ejs %>
<div id="page-wrapper">
    <div class="row">
        <div class="col-lg-12">
            <h1 class="page-header">代理/用户管理</h1>
            <!-- <button onclick="f()">123333</button> -->
        </div>
        <!-- /.col-lg-12 -->
    </div>
    <div class="row">
        <table id="userTable" class="table dataTable datatable_ table-striped table-bordered table-condensed">
            <thead>
                <th>id</th>
                <th>代理名</th>
                <th>性别</th>
                <th>年龄</th>
                <th>照片</th>
                <th>地址</th>
                <th>优惠次数</th>
                <th>注册时间</th>
                <th>邀请码</th>
                <th>佣金</th>
                <th>提成状态</th>
                <th>累计提成金额</th>
                <th>操作</th>
                <!--<th>描述</th>-->
            </thead>
            <tbody></tbody>
        </table>
    </div>
</div>
<!-- /#page-wrapper -->


</div>
<!-- /#wrapper -->

<!--修改用户信息模态框-->
<div class="modal fade" id="userUpdateModel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog">
        <!--modal-lg-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">修改用户信息</h4>
            </div>
            <div class="modal-body" style="min-height:350px">
                <form class="form-horizontal" role="form" id="updateUserForm">
                    <div class="form-group">
                        <label for="name" class="col-sm-3 control-label">代理名</label>
                        <div class="col-sm-9">
                            <input autocomplete="off" type="text" class="form-control" id="name"
                                   placeholder="请输入名字">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="sex" class="col-sm-3 control-label">性别</label>
                        <div class="col-sm-9">
                            <div class="radio-inline" col-sm-3>
                                <label>
                                    <input type="radio" name="optionsRadios" id="optionsRadios1" value="男" checked>男
                                </label>
                            </div>
                            <div class="radio-inline" col-sm-3>
                                <label>
                                    <input type="radio" name="optionsRadios" id="optionsRadios2" value="女">女
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="age" class="col-sm-3 control-label">年龄</label>
                        <div class="col-sm-9">
                            <input autocomplete="off" type="text" class="form-control" id="age"
                                   placeholder="请输入年龄">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputfile" class="col-sm-3 control-label">头像重新上传</label>
                        <input autocomplete="off" class="col-sm-9" type="file" id="inputfile">
                        <p id="oinputfile" class="col-sm-9" class="help-block"><a target="_blank" href="">原头像</a></p>
                    </div>
                    <div class="form-group">
                        <label for="address" class="col-sm-3 control-label">地址</label>
                        <div class="col-sm-9">
                            <input autocomplete="off" type="text" class="form-control" id="address"
                                   placeholder="请输入地址">
                        </div>
                    </div>
                    <!-- <div class="form-group">
                        <label for="grade" class="col-sm-3 control-label">级别</label>
                        <div class="col-sm-9">
                            <input autocomplete="off" type="text" class="form-control" id="grade"
                                   placeholder="请输入级别">
                        </div>
                    </div> -->
                    <!--<div class="form-group">-->
                        <!--<label for="cardid" class="col-sm-3 control-label">身份证号</label>-->
                        <!--<div class="col-sm-9">-->
                            <!--<input autocomplete="off" type="text" class="form-control" id="cardid"-->
                                   <!--placeholder="请输入身份证号">-->
                        <!--</div>-->
                    <!--</div>-->
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="confirmUpdateUser(this)">提交更改</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<% include footer.ejs %>
<script type="text/javascript">
    function f(){
        $.ajax({
            url: 'order/findAllOrder',
//            data:{
//                id:8
//            },
            method: 'post',
            success: function (res) {
                console.log(res)
            },
            error: function () {

            }
        })
    }
    $(function() {
        prodTableAjax()
    })
    function prodTableAjax(){
        $.ajax({
            url: 'user/findAllAction',
//            data:'',
            method: 'post',
            success: function (res) {
//                console.log(res)
                prodTable(res)
            },
            error: function () {

            }
        })
    }
    function prodTable(data){
        $('#userTable').DataTable({
            destroy: true,
            "bAutoWidth": false,
            responsive: true,
            data: data,
            "aoColumnDefs": [
                { "bSortable": false, "aTargets": [ 4,5,8,9] },
                {
                    "render": function(data, type, row) {
                        return (new Date(parseInt(data))).toLocaleString();
                    },
                    "targets": 7
                },
                {
                    "render": function(data, type, row) {
//                        console.log(data)
                        var html;
                        html = "<img src='"+data+"'>"
                        return html
                    },
                    "targets": 4
                },
                {
                    "render": function(data, type, row) {
                        if(data == null||data==0||data==""||data==undefined){
                            return '待申请'
                        }
                        else if(data == 1){
                            return '已申请'
                        }
                        else if(data == 2){
                            return '已批准'
                        }
                        else if(data == 3){
                            return '上笔提现已经完成'
                        }
                    },
                    "targets": 10
                },
                {
                    "render": function(data, type, row) {
                        var html;
                        html = "<button data-id="+data.id+" class='btn btn-sm btn-primary' data-toggle='modal' " +
                            "data-target='#userUpdateModel' onclick='userUpdateModel(this)'>修改</button>"+
                                "<button data-id="+data.id+" class='btn btn-sm btn-danger' onclick='deleteUser(this)" +
                            "'>删除</button>" +
                            "<button data-id="+data.id+" data-s="+data.perstatus+" class='btn btn-sm btn-info pizhun'" +
                            " " +
                            "onclick='pizhun(this)" +
                            "'>批准佣金</button>"
                        return html
                    },
                    "targets": 12
                },
            ],
            columns: [
                {
                    data: 'id'
                },{
                    data: 'name'
                },{
                    data: 'sex'
                },{
                    data: 'age'
                },{
                    data: 'icon'
                },{
                    data: 'address'
                },{
                    data: 'discount'
                },{
                    data: 'creationTime'
                },
                {
                    data: 'inviCode'
                },
                {
                    data: 'persent'
                },
                {
                    data: 'perstatus'
                },
                {
                    data: 'readyper'
                },
                {
                    data: null
                }
            ],
            language: {
                "sProcessing": "处理中...",
                "sLengthMenu": "显示 _MENU_ 项结果",
                "sZeroRecords": "没有匹配结果",
                "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
                "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
                "sInfoPostFix": "",
                "sSearch": "搜索:",
                "sUrl": "",
                "sEmptyTable": "表中数据为空",
                "sLoadingRecords": "载入中...",
                "sInfoThousands": ",",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "上页",
                    "sNext": "下页",
                    "sLast": "末页"
                },
                "oAria": {
                    "sSortAscending": ": 以升序排列此列",
                    "sSortDescending": ": 以降序排列此列"
                }
            },
            "fnCreatedRow": function (nRow, aData, iDataIndex) {
//                console.log(iDataIndex)
//                if(aData.perstatus == 1){
////                    console.log(1)
//                    $('.pizhun').eq(iDataIndex).hide()
//                }
            },
            "fnRowCallback": function (nRow, aaData, iDisplayIndex, iDisplayIndexFull) {

            }
        })
    }
    function userUpdateModel(btn){
//        var id =
        var id = $(btn).attr('data-id');
        $.ajax({
            url: 'user/findOneAction',
            data:{
                id:id
            },
            method: 'post',
            success: function (res) {
//                console.log(res)
                var data = res[0]
                $('#name').val(data.name)
                $('#name').attr('data-id',data.id)
                $('#name').attr('data-ct',data.creationTime)
                $('#inputfile').attr('data-icon',data.icon)
                $('#oinputfile').children('a').attr('href',data.icon)
                $("input:radio[name='optionsRadios'][value='" + data.sex + "']").prop("checked", "checked");
                $('#age').val(data.age)
                $('#address').val(data.address)
                $('#grade').val(data.grade)
//                $('#cardid').val(data.cardid)
            },
            error: function () {

            }
        })

    }
    function confirmUpdateUser(btn){
        var inputfile;
        if($('#inputfile').val()==""){
            inputfile = $('#inputfile').attr('data-icon')
        }
        else{
            inputfile = $('#inputfile').val()
        }
        var data = {
            name:$('#name').val(),
            id:$('#name').attr('data-id'),
            sex:$("input:radio[name='optionsRadios']:checked").val(),
            age:$('#age').val(),
            address:$('#address').val(),
            grade:$('#grade').val(),
//            cardid:$('#cardid').val(),
            creationTime:$('#name').attr('data-ct'),
            icon:inputfile
        }
        $.ajax({
            url: 'user/updateOneInfoAction',
            data:data,
            method: 'post',
            success: function (res) {
                alert(res.msg)
                prodTableAjax()
            },
            error: function () {

            }
        })
    }
    function deleteUser(btn){
//        alert("2")
        var id = $(btn).attr('data-id');
        $.ajax({
            url: 'user/deleteUserById',
            data:{
                id:id
            },
            method: 'post',
            success: function (res) {
                alert(res.msg)
//                console.log(res)
                prodTableAjax()
            },
            error: function () {

            }

        })
    }
    function pizhun(btn){
//        alert("2")
        var id = $(btn).attr('data-id');
        var s = $(btn).attr('data-s');
        if(s!=1){
            alert('目前不是申请状态，无法批准')
        }
        else {
            $.ajax({
                url: 'user/persent2',
                data: {
                    id: id
                },
                method: 'post',
                success: function (res) {
                    alert('成功')
                    console.log(res)
                    prodTableAjax()
                },
                error: function () {

                }

            })
        }
    }
    function uploadByForm() {
        //用form 表单直接 构造formData 对象; 就不需要下面的append 方法来为表单进行赋值了。
        var formData = new FormData($("#myForm")[0]);
        console.log(formData)
        var url = "user/uploader";
        $.ajax({
            url: url,
            type: 'POST',
            data: formData,

            /**
             * 必须false才会避开jQuery对 formdata 的默认处理
             * XMLHttpRequest会对 formdata 进行正确的处理
             */
            processData: false,
            /**
             *必须false才会自动加上正确的Content-Type
             */
            contentType: false,
            success: function (res) {
                console.log(res)
                alert(res.newPath);
                $("img").attr({"src": res.newPath}).prependTo($("body"));
            },
            error: function (res) {
                console.log(res)
                alert(res.newPath);
            }
        });
    }
</script>
</html>